package com.example.texted.callback;

import javax.swing.*;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;

public class MainA {
    private String a = "11";

    public static void main(String[] args) throws InterruptedException {
//
//        XiaoMing x = new XiaoMing();
//
//        XiaoFang xiaofang = new XiaoFang(x);
//
//        new Thread(xiaofang).start();
//
//        System.out.println("小明问完就出门买东西了");
//        String a1 = a;
//        z1.zz((res) -> {
//            System.out.println("zz()" + a);
//            return res;
//        });
//        fetchPrice();
        System.out.println("123123");
        MainA ma = new MainA();
        ma.a = "456";
        ma.fetchPrice();
        System.out.println("321321");
    }

    void fetchPrice() throws InterruptedException {
        ws.initWs((res) -> {
            System.out.println("zz()" + res + this.a);
            return res;
        });
    }
}

class z2 {
    private String b = "11";

    public static void main(String[] args) throws InterruptedException {

        String heroPosList ="0|0|0|0|0|0";
        String replace = heroPosList.replaceFirst("0", "1");
        System.out.println(replace);
//
//        XiaoMing x = new XiaoMing();
//
//        XiaoFang xiaofang = new XiaoFang(x);
//
//        new Thread(xiaofang).start();
//
//        System.out.println("小明问完就出门买东西了");
//        String a1 = a;
//        z1.zz((res) -> {
//            System.out.println("zz()" + a);
//            return res;
//        });
//        fetchPrice();


//        System.out.println("123123");
//        z2 Z2 = new z2();
//        Z2.b = "456";
//        Z2.fetchPrice();
//        System.out.println("321321");
//        long l = System.currentTimeMillis();
//        //迭代
//        Stream.iterate(1, n -> n + 1)
//                .limit(10000)
//                .parallel().forEachOrdered(System.out::println);
//        System.out.println(System.currentTimeMillis() - l);
    }

    void fetchPrice() throws InterruptedException {
        ws.initWs((res) -> {
//            System.out.println("zz()" + res + this.b);
            return res;
        });
    }
}

class ws {
    private static Map<Integer, Function<Double, Object>> cb;

    static double initWs(Function<Double, Object> fn) throws InterruptedException {
        cb.put(1, fn);
        TimeUnit.SECONDS.sleep(2);
        onMsg();
        return 0.3;
    }

    static void onMsg() {
        Function<Double, Object> doubleObjectFunction = cb.get(1);
        cb.remove(1);
        doubleObjectFunction.apply(0.666);
    }
}
